//
//  RetweedView.m
//  01 微博
//
//  Created by zhoup on 16/5/28.
//  Copyright © 2016年 zhoup. All rights reserved.
//

#import "RetweedView.h"

@interface RetweedView()

/**
 *  转发正文
 */
@property (weak, nonatomic) IBOutlet UILabel *retweetedContnetLabel;

/**
 *  转发配图管理容器
 */
@property (weak, nonatomic) IBOutlet UIImageView *retweetedView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *picHeight;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomHeight;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *retweetedHeight;
@end

@implementation RetweedView

- (void)setStatus:(IWStatus *)status
{
    _status = status;
    
    IWStatus *retweetedStatus = _status.retweeted_status;
    if (retweetedStatus != nil)
    {
        // 有转发, 显示转发
        self.picHeight.constant = 70;
        self.bottomHeight.constant = 10;
        [self retweetedStatus];
    }
    else
    {
        self.retweetedHeight.constant = 0;
        self.picHeight.constant = 0;
        self.bottomHeight.constant = 0;
    }
    
}


- (void)retweetedStatus
{
    IWStatus *retweetedStatus = _status.retweeted_status;
    // 1.设置转发正文
    self.retweetedContnetLabel.text = [NSString stringWithFormat:@"@%@:%@", retweetedStatus.user.name, retweetedStatus.text];
    
    CGSize size = CGSizeMake(self.retweetedContnetLabel.width, CGFLOAT_MAX);
    
    NSStringDrawingOptions opts = NSStringDrawingUsesLineFragmentOrigin;
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineBreakMode:NSLineBreakByCharWrapping];
    
    NSDictionary *attributes = @{ NSFontAttributeName : self.retweetedContnetLabel.font, NSParagraphStyleAttributeName : style };
    
    CGRect rect = [self.retweetedContnetLabel.text boundingRectWithSize:size
                                     options:opts
                                  attributes:attributes
                                     context:nil];
    
    NSLog(@"size == %lf",rect.size.height);
    self.retweetedHeight.constant =rect.size.height +10;
    
  

    
    // 2.设置数据
    if (_status.retweeted_status.pic_urls.count >0) {
        
        NSDictionary *photo = _status.retweeted_status.pic_urls[0];
        
        NSURL *imageUrl = [NSURL URLWithString:photo[@"thumbnail_pic"]];
        
        // 1.下载图片
        [self.retweetedView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
    }
    else
    {
        self.picHeight.constant = 0;
    }

    
}
@end
